<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.URLConnection"%>
<%@page import="java.io.OutputStreamWriter"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.io.IOException"%>

<%
	String esntlId = "10000044";
	String authKey = "4bcff97aedbd4723";
	String pageIndex = "1";
	String pageUnit = "10";
	String minY = "";
	String minX = "";
	String maxY = "";
	String maxX = "";
	String searchValue = "";
	String[] writngTrgetDscds = {"09", "22", "18"};
	String xmlUseYN = "";
	
	String urlStr = "http://www.safe182.go.kr/api/lcm/safeMap.do";
	
	String params = "esntlId=";
	params += esntlId;
	params += "&authKey=";
	params += authKey;
	params += "&pageIndex=";
	params += pageIndex;
	params += "&pageUnit=";
	params += pageUnit;
	params += "&minY=";
	params += minY;
	params += "&minX=";
	params += minX;
	params += "&maxY=";
	params += maxY;
	params += "&maxX=";
	params += maxX;
	params += "&searchValue=";
	params += searchValue;
	
	for(int i=0; i<writngTrgetDscds.length; i++){
		params += "&writngTrgetDscds=";
		params += writngTrgetDscds[i];	
	}
	
	params += "&xmlUseYN=";
	params += xmlUseYN;
	
	URL url;
	URLConnection connection;
	OutputStreamWriter wr = null;
	InputStreamReader isr = null;
	BufferedReader br = null;
	
	String result = "";
	
	try{
		url = new URL(urlStr);
		connection = url.openConnection();
		connection.setDoOutput(true);
		
		wr = new OutputStreamWriter(connection.getOutputStream());
		wr.write(params);
		wr.flush();
		
		isr = new InputStreamReader(connection.getInputStream(), "utf-8");
		br = new BufferedReader(isr);
		String buf = "";
	
		while(true){
			 buf = br.readLine();
			 if(buf == null){
				 break;
			 }else{
				 result = buf;
			 }
		 }
		
	 } catch(MalformedURLException e){
		 e.printStackTrace();
	 } catch(IOException ioe){
		 ioe.printStackTrace();
	 } finally {
		 wr.close();
		 isr.close();
		 br.close();
	 }
	 
%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>안전지도 정보 OPEN API 테스트</title>
<style type="text/css">
body {margin:0px; padding:0px; font-size:12px; font-family:"돋움",Dotum,Helvetica,AppleGothic,Sans-serif; line-height:1.5em; color:#666;}
</style> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
	var resultStr = '<%= result %>';
	var jsonObject = jQuery.parseJSON(resultStr.replace(/\s/g, "<br />"));
	
	var _result = jsonObject.result;
	var _msg = jsonObject.msg;
	var _totalCount = jsonObject.totalCount;
	var _list = jsonObject.list;
	
	document.write('<p> result : ' + _result + ', msg : ' + _msg + ', totalCount : ' + _totalCount + '</p>');
	document.write('<table border="1" width="100%">');
	document.write('	<tr>');
	document.write('		<th>안전시설명</th>');
	document.write('		<th>안전시설 우편번호</th>');
	document.write('		<th>안전시설 일련번호</th>');
	document.write('		<th>순번</th>');
	document.write('		<th>안전시설 주소</th>');
	document.write('		<th>기타주소</th>');
	document.write('		<th>분류코드</th>');
	document.write('		<th>안전시설 위도</th>');
	document.write('		<th>안전시설 전화번호</th>');
	document.write('		<th>안전시설 경도</th>');
	document.write('		<th>분류명</th>');
	document.write('	</tr>');
	
	for(var i=0; i<_list.length; i++){
		document.write('	<tr>');
		document.write('		<td>' + _list[i].bsshNm + '</td>');
		document.write('		<td>' + _list[i].zip + '</td>');
		document.write('		<td>' + _list[i].lcSn + '</td>');
		document.write('		<td>' + _list[i].rn + '</td>');
		document.write('		<td>' + _list[i].adres + '</td>');
		document.write('		<td>' + _list[i].etcAdres + '</td>');
		document.write('		<td>' + _list[i].cl + '</td>');
		document.write('		<td>' + _list[i].lcinfoLa + '</td>');
		document.write('		<td>' + _list[i].telno + '</td>');
		document.write('		<td>' + _list[i].lcinfoLo + '</td>');
		document.write('		<td>' + _list[i].clNm + '</td>');
		document.write('	</tr>');
	}
	
	document.write('</table>');

</script>
</head> 
<body>
</body>
</html>
